package org.streets.eis.component.grid;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.string.Strings;

import com.inmethod.grid.SizeUnit;
import com.inmethod.grid.column.AbstractColumn;
import com.inmethod.grid.common.AbstractGrid;

public class LinkColumn extends AbstractColumn {

	private static final long serialVersionUID = 1L;

	public LinkColumn(String columnId, IModel<?> headerModel) {
		super(columnId, headerModel);		
		setResizable(false);
		setSizeUnit(SizeUnit.PX);
		setInitialSize(60);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Component newCell(WebMarkupContainer parent, String componentId, final IModel rowModel) {
		return new LinkPanel(componentId, rowModel, getGrid()) {			
			private static final long serialVersionUID = 1L;

			@Override
			protected CharSequence getLinkLabel(IModel<?> rowModel){
				return LinkColumn.this.getLinkLabel(rowModel);
			}
			
			@Override
			protected void onLinkClicked(IModel<?> rowModel,AjaxRequestTarget target) {
				LinkColumn.this.onLinkClicked(rowModel,target);
			}		
		};
	}

	@SuppressWarnings("unchecked")	
	@Override
	public boolean cellClicked(IModel rowModel) {
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getCellCssClass(IModel rowModel, int rowNum) {
		return "imxt-want-prelight";
	}
	
	public abstract class LinkPanel extends Panel {
		private static final long serialVersionUID = 1L;
		
		@SuppressWarnings("unchecked")
		public LinkPanel(String id, final IModel<?> rowModel,final AbstractGrid grid) {
			super(id);			
			add(new AjaxLink("link") {				
				private static final long serialVersionUID = 1L;
				@Override
				public void onClick(AjaxRequestTarget target) {
					onLinkClicked(rowModel,target);
				}		
				@Override
				public boolean isVisible() {
					return grid.isItemEdited(rowModel);
				}
				@Override
				protected void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag){
					CharSequence label = getLinkLabel(rowModel); 
					if (!Strings.isEmpty(label)){
						replaceComponentTagBody(markupStream, openTag,label);
					}else{
						super.onComponentTagBody(markupStream, openTag);
					}				
				}
			});
		}
		
		protected abstract CharSequence getLinkLabel(IModel<?> rowModel);
		protected abstract void onLinkClicked(IModel<?> rowModel,AjaxRequestTarget target);

	};
	
	protected CharSequence getLinkLabel(IModel<?> rowModel){
		return getHeaderModel().getObject()!=null ? getHeaderModel().getObject().toString() : "";
	}
	
	protected void onLinkClicked(IModel<?> rowModel,AjaxRequestTarget target){
	}
	
}
